<?php
/**
 * 链接地址处理类
 * 
 * @version $Id: Url.php 183 2011-05-19 03:42:06Z linsir123 $
 */
class lpUrl
{
	/**
	 * 根据控制器和控制器方法构造链接
	 */
	static function __($route = '', $params = array(), $full = false)
	{
		$url = $route;
		App::instance()->urlMode == URL_REWRITE or $url = basename($_SERVER['SCRIPT_NAME']).'/'.$url;
		if (is_array($params)) {
			foreach ($params as $key => $val) 
				$url .= '/'.rawurlencode($key).'/'.rawurlencode($val);
		}
		
		///
		$domain = App::getRequest()->domain;
		if ($full) 
			$domain = "http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$domain;
		
		return $domain.$url;
	}
	
	/**
	 * 打印出根据控制器和控制器方法构造链接
	 */
	static function _e($route = '', $params = array(), $full = false)
	{
		echo self::__($route, $params, $full);
	}
	
	/**
	 * 跳转到根据控制器和控制器方法构造链接
	 */
	static function _r($route = '', $params = array(), $full = false)
	{
		$url = self::__($route, $params, $full);
		self::redirect($url);
	}
	
	/**
	 * 跳转到指定的链接
	 */
	static function redirect($url, $delay = 0)
	{
	    if (headers_sent() || $delay > 0) 
        	echo '<html><head><meta http-equiv="refresh" content="', $delay, ';URL=', $url, '" /></head></html>';
        else 
            header("Location:".$url);
	    
	    ///
	    exit();
	}
	
	/**
	 * 提供Smarty中构造链接的扩展函数
	 * 
	 * 使用说明：其中a和c分别为控制器和控制器方法键值，不能随便改变
	 * [code]
	 * <{url r="index/index" arg1="" arg2="" ...}>
	 * [/code]
	 */
	static function smartyUrl($params)
	{
		$route = isset($params['r']) ? rawurlencode($params['r']) : '';
		$params = array_diff($params, array($route));
		return self::__($route, $params);
	}
}
